package engine;

import java.util.ArrayList;
import java.util.List;


/**
 * LinearLevelManager.java
 * Implementation of LevelFactory where levels are traversed and ordered
 * sequentially
 * 
 * @author Connor Gordon
 * 
 */
public class LinearLevelManager extends LevelManager {

    private static List<Level> ourLevels;
    private static int ourIndex = 0;

    /**
     * Add new Level to end of order
     * 
     * @param l new Level
     */
    @Override
    public void addLevel (Level l) {
        if (ourLevels == null) {
            ourLevels = new ArrayList<Level>();
        }
        ourLevels.add(l);
        System.out.println("added level");
    }

    /**
     * Returns the level that preceded the current level
     * 
     * @return previous level
     */
    public Level getPreviousLevel () {
        ourIndex--;
        try {
            return ourLevels.get(ourIndex);
        }
        catch (IndexOutOfBoundsException e) {
            System.out.println("No level precedes this level");
        }
        return null;
    }

    /**
     * Goes to next level if player has won
     * 
     * @param pl current user playstate
     * @return next level
     */
    @Override
    public Level getNextLevel (PlayState pl) {
        try {
            if (pl == PlayState.START || pl == PlayState.HAS_WON) {
                Level next = ourLevels.get(ourIndex);
                ourIndex++;
                return next;
            }
            else // ourLevels.get(ourIndex).reset();
            return ourLevels.get(ourIndex);
        }
        catch (IndexOutOfBoundsException e) {
            return null;
        } // null return signifies game over

    }

}
